package Class;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 *
 * @author Edwin, Cristian
 */

public class Utils {
    
  public static String elementToString(Node n) {
    String name = n.getNodeName();

    short type = n.getNodeType();

    if (Node.CDATA_SECTION_NODE == type) {
      return "&lt;![CDATA[" + n.getNodeValue() + "]]&gt;";
    }

    if (name.startsWith("#")) {
      return "";
    }

    StringBuilder sb = new StringBuilder();
    sb.append("&lt;").append(name);

    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
      for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
      }
    }
    sb.append("/&gt;");

    return sb.toString();
  }
}